package com.ifengchao.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;
import org.apache.solr.client.solrj.SolrServer;
import org.apache.solr.common.SolrInputDocument;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ifengchao.dao.iface.SolrSearchDao;
import com.ifengchao.dao.iface.SolrSearchUserDao;
import com.ifengchao.pojo.HForumPost;
import com.ifengchao.service.SolrPostingSearchService;

/**
 * 搜索实现
 *
 * @author loki.xu
 * @version V1.0	
 * 
 * @date 2018年1月9日 下午2:28:12
 */
@Service
public class SolrPostingSearchServiceImpl implements SolrPostingSearchService {

	private static final Logger logger = Logger.getLogger(SolrPostingSearchServiceImpl.class);

	@Autowired(required = true)
	private SolrSearchDao solrSearchDao;
	
	@Autowired(required = true)
	private SolrSearchUserDao solrSearchUserDao;
	
	@Autowired(required = true)
	private SolrServer solrServer;
	
	@Override
	public void pushPostingSolrSearch() {
		// 是否要check开关
		
		try {
			List<HForumPost> postList = new ArrayList<HForumPost>();
			postList = solrSearchDao.getPostList();
			System.out.println("pushPostingSolrSearch postList:" + postList.size());
			logger.info("pushPostingSolrSearch postList:" + postList.size());
			
			if (postList != null && postList.size() > 0) {
				for (int i = 0; i < postList.size(); i++) {
					System.out.println(postList.get(i).getPostId());
					logger.info("pushPostingSolrSearch postId:" + postList.get(i).getPostId());
					
					// 先删除原来已存在solr的帖子数据
					solrServer.deleteById("P" + postList.get(i).getPostId());
					solrServer.commit();
					
					// 重新推送 
					SolrInputDocument document = new SolrInputDocument();
					document.addField("id", "P" + postList.get(i).getPostId());
					document.addField("user_id", postList.get(i).getUserId());
					document.addField("post_id", postList.get(i).getPostId());
					
					//HUser user = solrSearchUserDao.getHUser(String.valueOf(postList.get(i).getUserId()));
					//if (user != null) {
					//	document.addField("nickname", user.getNickName());
					//	document.addField("user_portrait", user.getUserPortrait());
					//} else {
						document.addField("nickname", "");
						document.addField("user_portrait", "");
					//}
					
					document.addField("posting_time", postList.get(i).getPostingTime());
					document.addField("area", postList.get(i).getArea());
					document.addField("title", postList.get(i).getTitle());
					document.addField("pic", postList.get(i).getPic());
					document.addField("read_count", postList.get(i).getReadCount());
					document.addField("collection_count", postList.get(i).getCollectionCount());
					document.addField("love_count", postList.get(i).getLoveCount());
					document.addField("follow_count", postList.get(i).getFollowCount());
					document.addField("attention_count", postList.get(i).getAttentionCount());
					document.addField("content", postList.get(i).getContent());
					document.addField("pic_original", postList.get(i).getPicOriginal());
					document.addField("select_type", "posting");
					
					solrServer.add(document);
					
					solrServer.commit();
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("pushPostingSolrSearch Exception:" + e);
		}
		System.out.println("pushPostingSolrSearch 正常结束");
		logger.info("pushPostingSolrSearch 正常结束");
	}

}
